﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HelloASPNET.TruyXuatDuLieu;
using System.Data;

namespace HelloASPNET.Admin
{
  public partial class TaoTaiKhoan : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnTaoTaiKhoan_Click(object sender, EventArgs e)
    {
      if (IsValid == false) //neu 1 trong cac validator kiem tra khong hop le thi IsValid ==false
      {
        return;
      }
      string username = txtUsername.Text;
      string hoTen = txtHoTen.Text;
      bool gioiTinhNam = radGioiTinhNam.Checked;
      DateTime ngaySinh = DateTime.ParseExact(txtNgaySinh.Text, "d/M/yyyy",
                                              new System.Globalization.CultureInfo("fr-FR"));
      string password = txtPassword.Text;

      string cauLenhSqlInsert =
        "INSERT INTO NguoiDung([Username],[Password],HoTen,GioiTinh,NgaySinh) "
        + " VALUES(@Username,@Password,@HoTen,@GioiTinh,@NgaySinh)";
      //int ketQua = TienIchSQL.ChayLenhCapNhatDuLieu(cauLenhSqlInsert,
      //        new string[] { "@Username", "@Password", "@HoTen", "@GioiTinh", "@NgaySinh" },
      //        new object[] { username, password, hoTen, gioiTinhNam, ngaySinh }
      //      );
      int ketQua = TienIchSQL.ChayLenhUpdateDuLieu(cauLenhSqlInsert,
                                                  "@Username", username
                                                  , "@Password", password
                                                  , "@HoTen", hoTen
                                                  , "@GioiTinh", gioiTinhNam
                                                  , "@NgaySinh", ngaySinh);
      lblThongBao.Text = "Số dòng được thêm vào là: " + ketQua.ToString();
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
      string username = txtUsername.Text;
      string sqlSelect = "SELECT * FROM NguoiDung WHERE [Username]=@Username";
      //string[] arrTenThamSo =new string[]{"@Username"};
      //object[] arrGiaTriThamSo = new object[]{username};
      //DataTable tblKetQua = TienIchSQL.ChayLenhSelectDuLieu(sqlSelect
      //                                                        , arrTenThamSo
      //                                                        , arrGiaTriThamSo);
      DataTable tblKetQua = TienIchSQL.ChayLenhSelectDuLieu(sqlSelect, "@Username", username);
      if (tblKetQua.Rows.Count > 0)
        args.IsValid = false;
      else
        args.IsValid = true;
    }
  }
}